package com.desksoft.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.desksoft.common.UUIDGenerator;
import com.desksoft.entity.Article;
import com.desksoft.entity.ArticleType;
import com.desksoft.service.ArticleService;
import com.desksoft.service.ArticleTypeService;
import com.desksoft.vo.ArticleVo;

/**
 * 文章
 * @author forever
 *
 */
@Controller
@RequestMapping(value="/page/article")
public class ArticleController {

	@Autowired
	private ArticleService  articleService;
	
	@Autowired
	ArticleTypeService articleTypeService;
	
	
	@RequestMapping(value="/initEidtPage")
	public String initUpload(HttpServletRequest request){
		List<ArticleType> articeTypeList = 	articleTypeService.queryAllArticelType();
		request.setAttribute("articeTypeList", articeTypeList);
		return "/page/edit.jsp";
	}
	
	
	/**
	 * 上传文章
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/add",method=RequestMethod.POST)
	@ResponseBody
	public Object addArticle(HttpServletRequest request,String articleTypeId ,String title ,String content,String keyUserName ,String imageUrl){
		Article article = new Article();
		article.setUserId("0000");
		article.setTitle(title);
		article.setContent(content);
		article.setPictureUrl(imageUrl);
		article.setKeyName(keyUserName);
		article.setArticleTypeId(articleTypeId);//伟人系列
		article.setArticleId(UUIDGenerator.get());
		articleService.insertArticle(article); 	 	
		return "1" ;
	}
	
	/**
	 * 根据前台条件查询文章
	 * @param request
	 * @param vo
	 * @return
	 */
	@RequestMapping(value="/ajax/query",method=RequestMethod.POST)
	public String queryArticleByCondition(HttpServletRequest request,ArticleVo vo){
		vo.setIsCupOf(true);
		List<Article> article =  articleService.queryArticleByCondition(vo);
		request.setAttribute("articleList",article);
		return "/ajaxreturn/article_list.jsp";
	}
	
	/**
	 * 文章页
	 * @param request
	 * @param articleId
	 * @return
	 */
	@RequestMapping(value="/articlePage",method=RequestMethod.GET)
	public String articlePage(HttpServletRequest request,String articleId){
		ArticleVo vo = new ArticleVo();
		vo.setArticleId(articleId);
		List<Article> articleList =  articleService.queryArticleByCondition(vo);
		
		Article result = new Article();
		if(articleList != null && articleList.size() != 0){
			result = articleList.get(0);
		}
		request.setAttribute("article",result);
		return "/page/article_page.jsp";
	}
}